<?php
/**
 * 发布求助
 * 
 */
 
include_once(GZ_PATH . '/includes/release_func.php');
class releaseAction extends Action{
	
	/**
	 * 发布事件
	*/
	public function release_info(){
		GZ_Api::authToken();
		$data=array();
	 
	 	$img_list = _POST('img_list');
		$coordinate = _POST('coordinate');
	 	$award_money = _POST('award_money');
		$data['user_id'] = _POST('user_id',0);
		$data['rescue_type'] = _POST('type','');
		$data['address'] = _POST('address','');
		$data['award_money'] = !empty($award_money)?floatval($award_money):0;//奖励金额
		$data['reality_money'] = $data['award_money'];//实付金额
		$data['incident_desc'] = _POST('incident_desc','');//情况描述
		$data['user_coordinate'] = json_encode($coordinate);
		$data['order_sn'] = date("YmdHis",time()).$data['user_id'];
		$data['add_time'] = time();
		
		if(empty($data['award_money'])){GZ_API::message("奖励金额不能为空",2);}
		if(empty($data['address'])){GZ_API::message("地址不能为空",2);}
		
		$count_img = count($img_list);
		if($count_img>0){
			$data['user_imglist'] = json_encode($img_list);
		}else{
			$data['user_imglist'] = '';
		}
		
		//检查实名认证
		$sql="SELECT user_rank,is_validated FROM " .$GLOBALS['db']->table('users').
		" WHERE user_id=".$data['user_id']." LIMIT 1";
		$res_user = $this->getRow($sql);
		if($res_user['user_rank']==1){
			GZ_Api::message("未实名认证",2);
		}
		//检查进行中事件
		/*$sql = "SELECT count(*) FROM " . $this->table("rescue_log") .
		" WHERE user_id=".$data['user_id']." AND status<2";
   		$count_rescue = $this->getOne($sql);
		if($count_rescue>0){
			GZ_Api::message("您还有进行中事件，不能发布新信息",2);
		}*/
		
     	$res = $this->autoExecute($GLOBALS["db"]->table("rescue_log"),$data,"INSERT");
		if($res){

          GZ_Api::message("发布成功",1);
		  
      	}else{
			
          GZ_Api::message("发布失败",2);
		  
      	}
	}
	
//发布详情
  public function release_detail(){
     GZ_Api::authToken();
     $user_id=_POST('user_id','');
     $id=_POST('log_id','');
     $type=1;//type 1:求助 2：救护
     $data=get_releaseInfo($id,$type);//求助详情
	 
	if(!empty($data)){
		GZ_Api::outPut($data); 
	}else{
		 GZ_Api::message("数据不存在",0);
	}
	 
     
  }
  //取消理由
  public function reason(){
     $res = $this->getAll('select id as reason_id,name as reason_name from '.$this->table('reason'));
     GZ_Api::outPut($res);
  }
  //取消发布
  public function release_cancel(){
     GZ_Api::authToken();
     $data=array();
     $user_id=_POST('user_id','');
     $id=_POST('log_id');
     $reason_id=_POST('reason_id');
     if(empty($reason_id)){GZ_API::message("请选择取消理由",2);}
     $data['reason']=$this->getOne('select name from '.$this->table('reason').' where id='.$reason_id);
     $data['status']=4;
     $res = $this->autoExecute($this->table('rescue_log'), $data, 'UPDATE', "id = ".$id);
     if($res){
          GZ_Api::message("取消成功",1);
      }else{
          GZ_Api::message("取消失败",2);
     }

  }
  //添加爱心费
	public function add_money(){
		GZ_Api::authToken();
		 
		
		$id = _POST('log_id','');//救护id
		$user_id = _POST('user_id',0);
		$up_money = _POST('up_money');
		 
		if(empty($up_money)){GZ_API::message("爱心费不能为空",2);}
	
		$where = ' where id='.$id. ' AND user_id='.$user_id;
		$rescue = $this->getRow('select user_id,award_money from '.$this->table('rescue_log').$where);
		 
		if($rescue){
			$data['markup_money'] = $up_money;
			$data['reality_money'] = $rescue['award_money']+$data['markup_money']; //实付金额
			$res = $this->autoExecute($this->table('rescue_log'), $data, 'UPDATE', "id = ".$id);
			if($res){
				GZ_Api::message("添加爱心费成功",1);
			}else{
				GZ_Api::message("添加失败",2);
			}
			 
		}else{
			GZ_Api::message("数据出错:信息不存在",0);
		}  
	}

}

?>